package com.yami.shop.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.model.Product;
import com.yami.shop.bean.model.ShopMember;
import com.yami.shop.bean.pt.YxPtsp;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.security.util.SecurityUtils;
import com.yami.shop.service.IYxPtspService;
import com.yami.shop.service.ProductService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/yxPtsp")
public class YxPtspController {

    @Autowired
    private IYxPtspService yxPtspService;
    @Autowired
    private ProductService productService;

    /**
     * 查询拼团商品列表
     */
    @GetMapping("/getPtProdList")
    public ResponseEntity<IPage<YxPtsp>> getPtProdList( PageParam<YxPtsp> page){
        YxPtsp yxPtsp = new YxPtsp();
        IPage<YxPtsp> yxPtspIPage =yxPtspService.selectYxPtspList(page,yxPtsp);
        return ResponseEntity.ok(yxPtspIPage);
    }

    /**
     * 新增拼团商品
     */
    @GetMapping("/addPtsp")
    public ResponseEntity<List<Product>> addPtsp(){
        Long shopId = SecurityUtils.getSysUser().getShopId();
        //查询所有上架的商品
        List<Product> productList = productService.getAllProduct(shopId);
        List<YxPtsp> list = yxPtspService.getYxPtspList(new YxPtsp());
        if(CollectionUtils.isNotEmpty(productList)){
            for(YxPtsp yxPtsp : list){
                for(Product product : productList){
                    if(product.getProdId().longValue()== yxPtsp.getGoodsid().longValue()){       // 两个里面 相同商品的 数据去掉
                        productList.remove(product);
                        break;
                    }
                }
            }
        }
        return ResponseEntity.ok(productList);
    }

    /**
     * 新增保存拼团商品
     */
    @PostMapping("/add")
    @ResponseBody
    public ResponseEntity<Void> add(YxPtsp yxPtsp){
        yxPtsp.setStatus("0");
        yxPtspService.insertYxPtsp(yxPtsp);
        return  ResponseEntity.ok().build();
    }

    /**
     * 修改拼团商品
     */
    @PostMapping("/updatePtsp")
    public ResponseEntity<Map>  updatePtsp(Long ptspid){
        Map map = new HashMap();
        YxPtsp yxPtsp = yxPtspService.selectYxPtspById(ptspid);
        map.put("yxPtsp",yxPtsp);
        if(yxPtsp!=null){
            Product product = productService.getProductByProdId(yxPtsp.getGoodsid());
            map.put("product",product);
        }
        return  ResponseEntity.ok(map);
    }

    /**
     * 修改保存拼团商品
     */
    @PostMapping("/savePtsp")
    public ResponseEntity<Void> savePtsp(YxPtsp yxPtsp){
        yxPtsp.setCreateTime(new Date());
        yxPtspService.updateYxPtsp(yxPtsp);
        return  ResponseEntity.ok().build();
    }

    /**
     * 删除拼团商品
     */
    @PostMapping("/deletePtsp")
    public ResponseEntity<Void> deletePtsp(String ids){
        yxPtspService.deleteYxPtspByIds(ids);
        return ResponseEntity.ok().build();
    }

}



